package com.zhuiyun.project.api.vipPlan.controller;

import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.plan.fitnessPurpose.service.FitnessPurposeService;
import com.zhuiyun.project.api.userrecord.mapper.UserRecordMapper;
import com.zhuiyun.project.api.userrecord.model.BodyFatModel;
import com.zhuiyun.project.api.userrecord.model.UploadUserRecordModel;
import com.zhuiyun.project.api.vipPlan.model.*;
import com.zhuiyun.project.api.vipPlan.service.VipPlanService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.bodyFatPercentageUtils;
import com.zhuiyun.project.utils.constants.CommonConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName VipPlanController
 * @Description TODO 教练端app学员管理控制层
 * @Author 杨友朋
 * @Date 2023/12/4 15:35
 **/
@RestController
@RequestMapping("/vipPlan")
@Api("教练端app学员管理控制层")
@Slf4j
public class VipPlanController {
    @Autowired
    VipPlanService vipPlanService;

    @Autowired
    FitnessPurposeService fitnessPurposeService;

    @Autowired
    UserRecordMapper userRecordMapper;

    /**
    * @Author 杨友朋
    * @Description 查询训练计划
    * @Date 2023/12/4 18:37
    * @Return CommonResult
    * @param coachId
    */
    @ApiOperation(value = "查询训练计划")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "查询训练计划")
    @GetMapping("selectPlan")
    public CommonResult selectArticle(int coachId){
        return vipPlanService.selectPlan(coachId);
    }

    /**
    * @Author 杨友朋
    * @Description 学员添加训练计划
    * @Date 2023/12/4 20:33
    * @Return CommonResult
    * @param addPlanModel
    */
    @ApiOperation(value = "学员添加训练计划")
 //   @OptLog(modular = "训练计划",type = CommonConstants.ADD,describe = "学员添加训练计划")
    @PostMapping("memberAddPlan")
    public CommonResult memberAddPlan(@RequestBody AddPlanModel addPlanModel){
        return vipPlanService.memberAddPlan(addPlanModel);
    }

    /**
     * @Author 杨友朋
     * @Description 学员查询训练动作
     * @Date 2023/12/4 20:33
     * @Return CommonResult
     * @param memberId
     */
    @ApiOperation(value = "学员查询训练动作")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "学员查询训练动作")
    @GetMapping("selectAction")
    public CommonResult selectAction(int memberId,String isComplete,String coachCode){
        return vipPlanService.selectAction(memberId,isComplete,coachCode);
    }

    /**
     * @Author 杨友朋
     * @Description 查询训练计划
     * @Date 2023/12/4 18:37
     * @Return CommonResult
     * @param coachId
     */
    @ApiOperation(value = "查询训练计划")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "查询训练计划")
    @GetMapping("selectSeries")
    public CommonResult selectSeries(int coachId,String venueUniqueIdentifier){
        return vipPlanService.selectSeries(coachId,venueUniqueIdentifier);
    }

    /**
    * @Author 杨友朋
    * @Description 查询时间
    * @Date 2023/12/6 10:18
    * @Return CommonResult
    * @param coachId,reservationTime
    */
    @ApiOperation(value = "查询时间")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "查询时间")
    @GetMapping("selectReservationTime")
    public CommonResult selectReservationTime(int coachId,String reservationTime){
        return vipPlanService.selectReservationTime(coachId,reservationTime);
    }

    /**
    * @Author 杨友朋
    * @Description 添加预约
    * @Date 2023/12/6 13:41
    * @Return CommonResult
    * @param reservationPlanModel
    */
    @ApiOperation(value = "添加预约")
    @OptLog(modular = "训练计划",type = CommonConstants.ADD,describe = "添加预约")
    @PostMapping("reservationPlan")
    public CommonResult reservationPlan(@RequestBody ReservationPlanModel reservationPlanModel) throws CommonException {
        return vipPlanService.reservationPlan(reservationPlanModel);
    }

    /**
    * @Author 杨友朋
    * @Description 查询所有预约计划
    * @Date 2023/12/7 10:21
    * @Return CommonResult
    * @param invitationCode,selectYear
    */
    @ApiOperation(value = "查询所有训练计划")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "添加预约")
    @GetMapping("selectAllPlan")
    public CommonResult selectAllPlan(@RequestParam String invitationCode, @RequestParam String selectYear,
                                      @RequestParam String isComplete, @RequestParam String selectMonth, String daiXunLianDate){
        return vipPlanService.selectAllPlan(invitationCode,selectYear,isComplete,selectMonth,daiXunLianDate);
    }

    @ApiOperation(value = "查询单个课程的动作")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "查询单个课程的动作")
    @GetMapping("selectActionByPlanTow")
    public CommonResult selectActionByPlanTow(int planId){
        return CommonResult.ok(vipPlanService.selectActionByPlan(planId));
    }

    @ApiOperation(value = "查询单个课程的动作")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "查询单个课程的动作")
    @GetMapping("selectActionByPlan")
    public CommonResult selectActionByPlan(int planId){
        return vipPlanService.selectActionByPlan(planId);
    }


    @ApiOperation(value = "查询的动作下组数")
    @OptLog(modular = "训练计划",type = CommonConstants.SELECT,describe = "查询的动作下组数")
    @GetMapping("selectActionByActionId")
    public CommonResult selectActionByActionId(int actionId){
        return vipPlanService.selectActionByActionId(actionId);
    }

    @ApiOperation(value = "删除动作下组数")
    @OptLog(modular = "训练计划",type = CommonConstants.DELETE,describe = "删除动作下组数")
    @GetMapping("removeActionCount")
    public CommonResult removeActionCount(int actionId){
        return vipPlanService.removeActionCount(actionId);
    }

    @ApiOperation(value = "添加的动作下组数")
    @OptLog(modular = "训练计划",type = CommonConstants.ADD,describe = "查询单个课程的动作")
    @PostMapping("insertActionCount")
    public CommonResult insertActionCount(@RequestBody ActionCountModel actionCountModel){
        return vipPlanService.insertActionCount(actionCountModel);
    }

    @ApiOperation(value = "修改的动作下组数")
    @OptLog(modular = "训练计划",type = CommonConstants.UPDATE,describe = "修改的动作下组数")
    @PostMapping("updateActionCount")
    public CommonResult updateActionCount(@RequestBody ActionCountModel actionCountModel){
        return vipPlanService.updateActionCount(actionCountModel);
    }

    @ApiOperation(value = "删除动作下组数")
    @OptLog(modular = "训练计划",type = CommonConstants.DELETE,describe = "删除动作下组数")
    @GetMapping("removeAction")
    public CommonResult removeAction(int actionId){
        return vipPlanService.removeAction(actionId);
    }

    @ApiOperation(value = "健身目的查询")
    @OptLog(modular = "健身目的列表", type = CommonConstants.SELECT, describe = "健身目的查询 ")
    @GetMapping("/queryFitnessPurpose")
    public CommonResult queryAll(String venueUniqueIdentifier){
        return CommonResult.ok(fitnessPurposeService.queryAll(venueUniqueIdentifier));
    }

    @ApiOperation(value = "健身类型查询")
    @OptLog(modular = "健身类型列表", type = CommonConstants.SELECT, describe = "健身类型查询 ")
    @GetMapping("/getFitnessType")
    public CommonResult getFitnessType(int fitnessPurposeId){
        return vipPlanService.getFitnessType(fitnessPurposeId);
    }

    @ApiOperation(value = "健身动作查询")
    @OptLog(modular = "健身类型列表", type = CommonConstants.SELECT, describe = "健身动作查询 ")
    @GetMapping("/getAllAction")
    public CommonResult getAllAction(int fitnessTypeId){
        return vipPlanService.getAllAction(fitnessTypeId);
    }
    @ApiOperation(value = "添加动作")
    @OptLog(modular = "训练计划",type = CommonConstants.ADD,describe = "添加动作")
    @PostMapping("insertAction")
    public CommonResult insertAction(@RequestBody InsertPlanActionModel planActionModel){
        return vipPlanService.insertAction(planActionModel);
    }
    @ApiOperation(value = "查询单个预约")
    @OptLog(modular = "健身类型列表", type = CommonConstants.SELECT, describe = "查询单个预约")
    @GetMapping("/selectReservationById")
    public CommonResult selectReservationById(int courseId){
        return vipPlanService.selectReservationById(courseId);
    }

    @ApiOperation(value = "取消预约")
    @OptLog(modular = "健身类型列表", type = CommonConstants.UPDATE, describe = "取消预约")
    @GetMapping("/cancelReservation")
    public CommonResult cancelReservation(int courseId,int studentId){
        return vipPlanService.cancelReservation(courseId,studentId);
    }
    /*
    * @Author wuwan
    * @Description 完成课程
    * @Date 2023/12/12 14:38
    * @Return
    */
    @ApiOperation(value = "完成课程")
    @OptLog(modular = "健身类型列表", type = CommonConstants.UPDATE, describe = "完成课程")
    @PostMapping("/completePlan")
    public CommonResult completePlan(@RequestBody AddPlanModel addPlanModel){
        return vipPlanService.completePlan(addPlanModel);
    }
    /*
    * @Author wuwan
    * @Description 上传课程
    * @Date 2023/12/12 14:38
    * @Return
    */
    @ApiOperation(value = "上传课程")
    @OptLog(modular = "健身类型列表", type = CommonConstants.UPDATE, describe = "上传课程")
    @PostMapping("/upCompletePlan")
    public CommonResult upCompletePlan(@RequestBody AddPlanModel addPlanModel){
        return vipPlanService.upCompletePlan(addPlanModel);
    }
    /*
    * @Author wuwan
    * @Description 添加案例图片
    * @Date 2023/12/12 11:37
    * @Return
    */
    @ApiOperation(value = "添加案例图片")
    @OptLog(modular = "添加案例图片", type = CommonConstants.UPDATE, describe = "添加案例图片")
    @PostMapping("/saveImageToDatabase")
    public CommonResult saveImageToDatabase(@RequestBody AddImageCaseModel addImageCaseModel){
        return CommonResult.ok(vipPlanService.saveImageToDatabase(addImageCaseModel));
    }
    /*
    * @Author wuwan
    * @Description 删除案例图片
    * @Date 2023/12/12 11:38
    * @Return
    */
    @ApiOperation(value = "删除案例图片")
    @OptLog(modular = "删除案例图片", type = CommonConstants.UPDATE, describe = "删除案例图片")
    @PostMapping("/DeleteImageToDatabase")
    public CommonResult DeleteImageToDatabase(@RequestBody AddImageCaseModel addImageCaseModel){
        return CommonResult.ok(vipPlanService.DeleteImageToDatabase(addImageCaseModel));
    }


    /**
    * @Author wx
    * @Description 根据教练id和租户标识查询时间
    * @Date 2023/12/11 14:18
    * @Return
    * @param id（教练id）
    */
    @ApiOperation(value = "根据教练id和租户标识查询时间")
    @OptLog(modular = "健身类型列表", type = CommonConstants.SELECT, describe = "根据教练id和租户标识查询时间")
    @GetMapping("/getSelectYear")
    public CommonResult getSelectYear(Integer id,String venueUniqueIdentifier){
        return CommonResult.ok(vipPlanService.getSelectYear(id,venueUniqueIdentifier));
    }

    /**
    * @Author wx 查询约课状态
    * @Description
    * @Date 2023/12/14 11:30
    * @Return
    * @param studentId
    */
    @ApiOperation(value = "查询约课状态")
    @OptLog(modular = "健身类型列表", type = CommonConstants.SELECT, describe = "查询约课状态")
    @GetMapping("/selectResStatus")
    public CommonResult selectResStatus(Integer courseId,Integer studentId,Integer coachId){
        return CommonResult.ok(vipPlanService.selectResStatus(courseId,studentId,coachId));
    }
    /**
     * @Author wuwan
     * @Description 查询是否约满
     * @Date 2023/12/14 11:30
     * @Return
     */
    @ApiOperation(value = "查询是否约满")
    @OptLog(modular = "查询是否约满", type = CommonConstants.SELECT, describe = "查询是否约满")
    @GetMapping("/getCount")
    public CommonResult getCount(@RequestParam("daiXunLianDate") String  daiXunLianDate,String invitationCode,Integer coachId){

        String trim = daiXunLianDate.substring(1, daiXunLianDate.length() - 1);
        String[] dateStrings = trim.split(",");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Date> dateList = new ArrayList<>();

        for (int i = 0; i < dateStrings.length; i++ ) {
            try {
                String replace = dateStrings[i].replace("\"", "");
                Date date = sdf.parse(replace);
                dateList.add(date);
            } catch (ParseException e) {
                // 处理日期解析异常
                e.printStackTrace();
            }
        }
        return CommonResult.ok(vipPlanService.getCount(dateList,invitationCode,coachId));
    }
}
